{include file="public/header" /}
<style type="text/css">
    .uploader-list {

        margin-left: -15px;

    }



    .uploader-list .info {

        position: relative;

        margin-top: -25px;

        background-color: black;

        color: white;

        filter: alpha(Opacity=80);

        -moz-opacity: 0.5;

        opacity: 0.5;

        width: 100px;

        height: 25px;

        text-align: center;

        display: none;

    }



    .uploader-list .handle {

        position: relative;

        background-color: black;

        color: white;

        filter: alpha(Opacity=80);

        -moz-opacity: 0.5;

        opacity: 0.5;

        width: 100px;

        text-align: right;

        height: 18px;

        margin-bottom: -18px;

        display: none;

    }

    .layui-table td,
    .layui-table th {
        min-width: 10px !important;
    }

    .uploader-list .handle i {

        margin-right: 5px;

    }



    .uploader-list .handle i:hover {

        cursor: pointer;

    }



    .uploader-list .file-iteme {

        margin: 12px 0 0 15px;

        padding: 1px;

        float: left;

    }
</style>

<body style="background-color: #ffffff">
    <div class="layui-fluid">
        <div class="layui-row">
            <form class="layui-form">
                <div class="layui-form-item">
                    <label for="L_choose_name" class="layui-form-label">
                        <span class="x-red">*</span>商品名称</label>
                    <div class="layui-input-inline">
                        <input type="text" id="title" name="title" required="" lay-verify="required" autocomplete="off"
                            class="layui-input" value="{$row.title|default=''}" placeholder="">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="L_choose_name" class="layui-form-label">
                        <span class="x-red">*</span>商品编号[唯一]</label>
                    <div class="layui-input-inline">
                        <input type="text" id="goodsSn" name="goodsSn" required="" lay-verify="required"
                            autocomplete="off" class="layui-input" value="{$row.goodsSn|default=''}" placeholder="">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="L_choose_name" class="layui-form-label">
                        <span class="x-red">*</span>所属商家分类</label>
                    <div class="layui-input-inline">
                        <select name="cate" lay-verify="" lay-search='' lay-filter="cate">
                            <option value="">输入名称快速查找</option>
                            {foreach $cate as $v}
                            <option value="{$v.id}" {if !empty($row.cate) && $row.cate==$v.id}selected{/if}>
                                {$v.name|default=''}</option>
                            {/foreach}
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">商品简述</label>
                    <div class="layui-input-inline">
                        <textarea name="descs" placeholder="请输入内容"
                            class="layui-textarea">{$row.descs|default=''}</textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="L_choose_name" class="layui-form-label">
                        是否上架</label>
                    <div class="layui-input-inline">
                        <select name="isSale" lay-verify="required" lay-filter="type">
                            <option value="1" {if !empty($row.isSale) && $row.isSale==1}selected{/if}>是</option>
                            <option value="2" {if !empty($row.isSale) && $row.isSale==2}selected{/if}>否</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="L_choose_name" class="layui-form-label">
                        是否推荐</label>
                    <div class="layui-input-inline">
                        <select name="isBest" lay-verify="required" lay-filter="type">
                            <option value="1" {if !empty($row.isBest) && $row.isBest==1}selected{/if}>是</option>
                            <option value="2" {if !empty($row.isBest) && $row.isBest==2}selected{/if}>否</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="L_choose_name" class="layui-form-label">
                        是否热卖</label>
                    <div class="layui-input-inline">
                        <select name="isHot" lay-verify="required" lay-filter="type">
                            <option value="1" {if !empty($row.isHot) && $row.isHot==1}selected{/if}>是</option>
                            <option value="2" {if !empty($row.isHot) && $row.isHot==2}selected{/if}>否</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="L_choose_name" class="layui-form-label">
                        是否新品</label>
                    <div class="layui-input-inline">
                        <select name="isNew" lay-verify="required" lay-filter="type">
                            <option value="1" {if !empty($row.isNew) && $row.isNew==1}selected{/if}>是</option>
                            <option value="2" {if !empty($row.isNew) && $row.isNew==2}selected{/if}>否</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="L_choose_name" class="layui-form-label">
                        <span class="x-red">*</span>商品关键字</label>
                    <div class="layui-input-inline">
                        <input type="text" id="Keywords" name="Keywords" required="" lay-verify="required"
                            autocomplete="off" class="layui-input" value="{$row.Keywords|default=''}"
                            placeholder="方便用户搜索">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="cover" class="layui-form-label">
                        <span class="x-red">*</span>商品图片</label>
                    <div class="layui-input-block" style="width: 80%;">
                        <div class="layui-upload">
                            <button type="button" class="layui-btn layui-btn-normal" id="test2">多图片上传</button>
                            <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;width: 88%">
                                预览图：
                                <div class="layui-upload-list uploader-list" style="overflow: auto;" id="uploader-list">
                                    {if !empty($row.images)}
                                    {foreach $row.images as $v}
                                    <div class="file-iteme">
                                        <div class="handle" style="display: none;">
                                            <i class="layui-icon layui-icon-delete"></i>
                                        </div><img style="width: 100px;height: 100px;" src="{$v}">
                                        <input type="hidden" name="images[]" value="{$v}">
                                    </div>
                                    {/foreach}
                                    {/if}
                                </div>
                            </blockquote>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="layeditDemo" class="layui-form-label">
                        商品规格</label>
                    <div class="layui-input-inline" style="width: 60%;">
                        <a class="layui-btn layui-btn-normal layui-btn-btn layui-btn-xs add"
                            onclick="addtable()">添加新规格</a>
                        <table class="layui-table" id="table" lay-filter="table">
                            <thead>
                                <tr>
                                    <td>规格名称</td>
                                    <td>市场价</td>
                                    <td>本店售价</td>
                                    <td>库存</td>
                                    <td>预警库存</td>
                                    <td>排序[值越大越靠前]</td>
                                    <td>操作</td>
                                </tr>
                            </thead>
                            <tbody>
                                {if !empty($row.speclist)}
                                {foreach $row.speclist as $k => $v}
                                <tr>
                                    <td><input required="" lay-verify="required" type="text" id="layui-input"
                                            class="layui-input" name="spec[{$k}][name]" value="{$v.name}">
                                    </td>
                                    <td><input required="" lay-verify="required" type="number" id="layui-input"
                                            class="layui-input" name="spec[{$k}][oprice]" value="{$v.oprice}">
                                    </td>
                                    <td><input required="" lay-verify="required" type="number" id="layui-input"
                                            class="layui-input" name="spec[{$k}][price]" value="{$v.price}">
                                    </td>
                                    <td><input required="" lay-verify="required" type="number" id="layui-input"
                                            class="layui-input" name="spec[{$k}][goodsstock]" value="{$v.goodsstock}">
                                    </td>
                                    <td><input required="" lay-verify="required" type="number" id="layui-input"
                                            class="layui-input" name="spec[{$k}][warnStock]" value="{$v.warnStock}">
                                    </td>
                                    <td><input required="" lay-verify="required" type="number" id="layui-input"
                                            class="layui-input" name="spec[{$k}][is_default]"
                                            value="{$v.is_default|default='0'}">
                                    </td>
                                    <td>
                                        <input type="hidden" value="{$v.id}" name="spec[{$k}][id]" />
                                        <a class="layui-btn layui-btn-danger layui-btn-xs del">删除规格</a>
                                    </td>
                                </tr>
                                {/foreach}
                                {else/}
                                <tr>
                                    <td><input required="" lay-verify="required" type="text" id="layui-input"
                                            class="layui-input" name="spec[0][name]" value="">
                                    </td>
                                    <td><input required="" lay-verify="required" type="number" id="layui-input"
                                            class="layui-input" name="spec[0][oprice]" value="0">
                                    </td>
                                    <td><input required="" lay-verify="required" type="number" id="layui-input"
                                            class="layui-input" name="spec[0][price]" value="0">
                                    </td>
                                    <td><input required="" lay-verify="required" type="number" id="layui-input"
                                            class="layui-input" name="spec[0][goodsstock]" value="0">
                                    </td>
                                    <td><input required="" lay-verify="required" type="number" id="layui-input"
                                            class="layui-input" name="spec[0][warnStock]" value="0">
                                    </td>
                                    <td><input required="" lay-verify="required" type="number" id="layui-input"
                                            class="layui-input" name="spec[0][is_default]" value="0">
                                    </td>
                                    <td>
                                        <input type="hidden" value="0" name="spec[0][id]" />
                                        <a class="layui-btn layui-btn-danger layui-btn-xs del">删除规格</a>
                                    </td>
                                </tr>
                                {/if}
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="layeditDemo" class="layui-form-label">
                        商品参数</label>
                    <div class="layui-input-inline" style="width: 50%;">
                        <a class="layui-btn layui-btn-btn layui-btn-xs add layui-btn-normal"
                            onclick="addtable2()">添加新参数</a>
                        <table class="layui-table" id="table2" lay-filter="table2">
                            <thead>
                                <tr>
                                    <td>参数名称</td>
                                    <td>参数值</td>
                                    <td>操作</td>
                                </tr>
                            </thead>
                            <tbody>
                                {if !empty($row.parpam)}
                                {foreach $row.parpam as $k => $v}
                                <tr>
                                    <td><input required="" lay-verify="required" type="text" id="layui-input"
                                            class="layui-input" name="parpam[{$k}][key]" value="{$v.key}">
                                    </td>
                                    <td><input required="" lay-verify="required" type="text" id="layui-input"
                                            class="layui-input" name="parpam[{$k}][value]" value="{$v.value}">
                                    </td>
                                    <td>
                                        <a class="layui-btn layui-btn-danger layui-btn-xs del2">删除参数</a>
                                    </td>
                                </tr>
                                {/foreach}
                                {/if}
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="layeditDemo" class="layui-form-label">
                        商品介绍</label>
                    <div class="layui-input-inline" style="width: 60%;">
                        <textarea id="infos"
                            name="goodsDesc">{if !empty($row.goodsDesc)}{:htmlspecialchars_decode($row.goodsDesc)}{/if}</textarea>
                    </div>
                </div>

                <div class="layui-form-item">
                    <input type="hidden" name='id' value="{$row.id|default='0'}" />
                    <label class="layui-form-label"></label>
                    <button class="layui-btn layui-btn-normal" lay-filter="add" lay-submit="">保存</button>
                    &nbsp;&nbsp;&nbsp;&nbsp;
                    <button type="button" class="layui-btn layui-btn-primary" onclick="IframeClose()">取消</button>

                </div>
            </form>
        </div>
    </div>

    {include file="public/footer" /}
    <script src="/static/leyedit/Layui-KnifeZ/layui.all.js"></script>
    <script src="/static/leyedit/ace/ace.js"></script>
    <script>
        function addtable2() {
            var i = $('#table2 tbody tr').length;
            //你要添加的html
            var html = '<tr>' +
                '<td><input required="" lay-verify="required" type="text" id="layui-input" class="layui-input" name="parpam[' + i + '][key]" value=""></td>' +
                '<td><input required="" lay-verify="required" type="text" id="layui-input" class="layui-input" name="parpam[' + i + '][value]" value=""></td>' +
                '<td><a class="layui-btn layui-btn-danger layui-btn-xs del2" >删除参数</a></td></tr>';
            $(html).appendTo($('#table2 tbody:last'));
            layui.use(['form'], function () {
                $ = layui.jquery;
                var form = layui.form;
                form.render();
            });
        };
        $('body').on('click', '.del2', function () {
            $(this).closest('tr').remove();
        });
    </script>
    <script>
        function addtable() {
            var i = $('#table tbody tr').length;
            //你要添加的html
            var html = '<tr>' +
                '<td><input required="" lay-verify="required" type="text" id="layui-input" class="layui-input" name="spec[' + i + '][name]" value=""></td>' +
                '<td><input required="" lay-verify="required" type="number" id="layui-input" class="layui-input" name="spec[' + i + '][oprice]" value=""></td>' +
                '<td><input required="" lay-verify="required" type="number" id="layui-input" class="layui-input" name="spec[' + i + '][price]" value=""></td>' +
                '<td><input required="" lay-verify="required" type="number" id="layui-input" class="layui-input" name="spec[' + i + '][goodsstock]" value=""></td>' +
                '<td><input required="" lay-verify="required" type="number" id="layui-input" class="layui-input" name="spec[' + i + '][warnStock]" value=""></td>' +
                '<td><input required="" lay-verify="required" type="number" id="layui-input" class="layui-input" name="spec[' + i + '][is_default]" value="0"></td>' +
                '<td><input required="" lay-verify="required" type="hidden" value="0" name="spec[' + i + '][id]" />' +
                '<a class="layui-btn layui-btn-danger layui-btn-xs del" >删除规格</a>' +
                '</td></tr>';
            $(html).appendTo($('#table tbody:last'));
            layui.use(['form'], function () {
                $ = layui.jquery;
                var form = layui.form;
                form.render();
            });
        };
        $('body').on('click', '.del', function () {
            var id = $(this).prev().val();
            var top = $(this).closest('tr');
            if ($('#table tbody tr').length === 1) {
                layer.msg('规格参数必须拥有一个', {
                    time: 2000
                });
                return false;
            }
            if (id && id > 0) {
                layer.confirm('该规格已存入数据库，删除后相关待付款订单会发生变化', { title: '确定删除吗？' }, function (index, object) {
                    //删除规格操作
                    $.ajax({
                        type: 'get',
                        url: '/shop/goodsspec_del',
                        data: { id: id },
                        success: function (res) {
                            if (res.code == 0) {
                                layer.msg(res.msg, { icon: 6, time: 1000 }, function () {
                                    top.remove();
                                    layer.close(index);
                                });
                            } else {
                                layer.msg(res.msg, { icon: 2, time: 1000 });
                            }
                        }
                    });

                });
                return false;
            }
            //删除当前按钮所在的tr
            $(this).closest('tr').remove();
        });
    </script>
    <script>
        // 删除图片
        $(document).on("click", ".file-iteme .handle", function (event) {
            $(this).parent().remove();
        });
        $(document).on("mouseenter mouseleave", ".file-iteme", function (event) {
            if (event.type === "mouseenter") {
                //鼠标悬浮
                $(this).children(".info").fadeIn("fast");
                $(this).children(".handle").fadeIn("fast");
            } else if (event.type === "mouseleave") {
                //鼠标离开
                $(this).children(".info").hide();
                $(this).children(".handle").hide();
            }
        });
        layui.use(['layer', 'jquery', 'form', 'upload', 'layedit', 'table'], function () {
            $ = layui.jquery;
            var form = layui.form
                , upload = layui.upload
                , layer = layui.layer;
            var layedit = layui.layedit;
            layedit.set({
                //暴露layupload参数设置接口 --详细查看layupload参数说明
                uploadImage: {
                    url: '/Base/upload_img',
                    accept: 'image',
                    acceptMime: 'image/*',
                    exts: 'jpg|png|gif|bmp|jpeg',
                    size: '100240'
                }
                //右键删除图片/视频时的回调参数，post到后台删除服务器文件等操作，
                //传递参数：
                //图片： imgpath --图片路径
                //视频： filepath --视频路径 imgpath --封面路径
                , calldel: {
                    url: '/Base/upload_img'
                }
                //开发者模式 --默认为false
                , devmode: false
                //插入代码设置
                , codeConfig: {
                    hide: true,  //是否显示编码语言选择框
                    default: 'javascript' //hide为true时的默认语言格式
                }

                , tool: [
                    'strong', 'italic', 'underline', 'del', '|', 'left', 'center', 'right', 'image'
                ]
                , height: '300px'
            });
            var demoListView = $('#demoList'),
                uploadListIns = upload.render({
                    elem: '#test2'
                    , url: '/Base/upload_img'
                    , multiple: true
                    , before: function (obj) {
                        layer.msg('图片上传中...', {
                            icon: 16,
                            shade: 0.01,
                            time: 0
                        })
                    }
                    , done: function (res) {
                        if (res.code == 0) {
                            //上传完毕
                            $('#uploader-list').append(
                                '<div id="" class="file-iteme">' +
                                '<div class="handle"><i class="layui-icon layui-icon-delete"></i></div>' +
                                '<img style="width: 100px;height: 100px;" src=' + res.data.src + '>' +
                                '<input type="hidden" name="images[]" value="' + res.data.src + '"></div>'
                            );
                        }
                        layer.close(layer.msg());//关闭上传提示窗口
                    }
                });
            form.on('select(shopid)', function (data) {
                $.ajax({
                    type: 'get',
                    url: '/base/gettypes',
                    data: { type: 2, shopid: data.value },
                    success: function (res) {
                        if (res.code == 0) {
                            str = '';
                            str += '<option value="">请选择</option>';
                            $.each(res.data, function (i, obj) {
                                str += '<option value="' + obj.id + '">' + obj.title + '</option>';
                            });
                            $('select[name="cate"]').html(str);
                            form.render();
                        } else {
                            str = '';
                            form.render();
                        }
                    }
                });
                return false;
            });
            //监听提交
            var infos = layedit.build('infos');
            form.on('submit(add)', function (data) {
                data.field.goodsDesc = layedit.getContent(infos);
                $.ajax({
                    'type': 'post',
                    data: data.field,
                    success: function (res) {
                        if (res.code == 0) {
                            layer.msg(res.msg, { icon: 6, time: 1000 }, function () {
                                // 获得frame索引
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.location.reload();
                                parent.layer.close(index);
                            });
                        } else {
                            layer.msg(res.msg, { icon: 2, time: 1000 });
                        }
                    }
                });
                return false;
            });
        });
    </script>

</body>

</html>